package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.OrderApply;
import com.xmy.cultivate.entity.OrderDisClan;
import com.xmy.cultivate.entity.OrderDisDetail;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.mapper.OrderDisDetailMapper;
import com.xmy.cultivate.mapper.StudentMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单优惠详情表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-12-19
 */
@Service
public class OrderDisDetailServiceImpl extends ServiceImpl<OrderDisDetailMapper, OrderDisDetail> implements IOrderDisDetailService {

    @Autowired
    @Lazy
    IDictService iDictService;

    @Autowired
    @Lazy
    IOrderDisPriceService iOrderDisPriceService;

    @Autowired
    IOrderDisClanService iOrderDisClanService;

    @Autowired
    IOrderDetailService iOrderDetailService;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    IOrderApplyService iOrderApplyService;

    @Override
    public boolean addDisDetaill(Long orderId, Long disOrderId, List<OrderDisDetail> orderDisDetailList, BigDecimal disPrice, LocalDate discountsDate){
        Long disId = iOrderDisPriceService.addDisPrice(orderId,disOrderId,disPrice,discountsDate);
        if (orderDisDetailList != null) {
            for (OrderDisDetail orderDisDetail : orderDisDetailList) {
                orderDisDetail.setOrderId(orderId);
                orderDisDetail.setDisPriceId(disId);
                orderDisDetail.setDisReasonName(iDictService.getDictNameForNum(orderDisDetail.getDisReasonId(), 24));
                this.save(orderDisDetail);
            }
        }
        return true;
    }

    @Override
    public boolean addDisDetailAndClan(List<Long> studentIdList, Long orderDetailId, Long orderId, Long disOrderId, List<OrderDisDetail> orderDisDetailList, BigDecimal disPrice, LocalDate discountsDate) {
        Long disId = iOrderDisPriceService.addDisPrice(orderId, disOrderId, disPrice, discountsDate);
        OrderApply orderApply = iOrderApplyService.getById(disOrderId);

        if (studentIdList != null && studentIdList.size() != 0) {
            // 已有的亲属id
            List<Long> kinsfolkList = studentMapper.getKinsfolkByStudent(orderApply.getStudentId());
            // 排除已添加的亲属
            studentIdList = studentIdList.stream()
                    .filter(id -> !kinsfolkList.contains(id))
                    .collect(Collectors.toList());
        }

        if (orderDisDetailList != null) {
            for (OrderDisDetail orderDisDetail : orderDisDetailList) {
                orderDisDetail.setOrderId(orderId);
                orderDisDetail.setDisPriceId(disId);
                orderDisDetail.setDisReasonName(iDictService.getDictNameForNum(orderDisDetail.getDisReasonId(), 24));
                this.save(orderDisDetail);

                if (orderDisDetail.getDisReasonId() == 3) {
                    for (Long studentId : studentIdList) {
                        OrderDisClan orderDisClan = new OrderDisClan();
                        orderDisClan.setOrderId(disOrderId);
                        orderDisClan.setOrderDetailId(orderDetailId);
                        orderDisClan.setOrderDisDetailId(orderDisDetail.getId());
                        orderDisClan.setStudentId(studentId);
                        iOrderDisClanService.save(orderDisClan);
                    }
                }
            }
        }
        return true;
    }


}
